package badDesign
{
import flash.utils.Dictionary;
/**
 * @author daichangxin
 */
public class Context
{
	public static const IDLE:String = "idle";
	public static const MOVE:String = "move";
	public static const ATTACK:String = "attack";
	public static const ATTACKED:String = "attacked";
	public static const DEATH:String = "death";
	
	private var _dic:Dictionary;
	private var _state:IState;
	
	public function Context()
	{
		_dic = new Dictionary();
	}
	
	private function getState(stateName:String):IState
	{
		return _dic[stateName];
	}
	
	public function registerState(state:IState):void
	{
		_dic[state.getName()] = state;
	}
	
	public function changeState(newStateName:String):void
	{
		if (_state != null && newStateName == _state.getName()) return;
		
		if (_state != null) _state.onExit();
		
		_state = getState(newStateName);
		_state.onEnter();
	}
	
	public function getCurrentState():IState
	{
		return _state;
	}
}
}
